define(function() {
	var eventObjective = function(project, fields, extTBName) {
		this.events = {};
		this.fields = fields;
		this.extTBName = extTBName;
		this.project = project;
	};

	eventObjective.prototype = {
		add : function(id, oId, otId, status, startTime) {
			var a = this, eoBoundData = new BoundData("EventObjective" + $E.staff.epidSql + (this.extTBName || ''));
            eoBoundData.notFindDupli = 1;
            eoBoundData.KeyInfoEdit("OBJECTIVE_GUID");
			a.setValue(eoBoundData, "EVENT_GUID", id);
			a.setValue(eoBoundData, "OBJECTIVE_GUID", oId);
			a.setValue(eoBoundData, "ObjectiveType_ID", otId);
			a.setValue(eoBoundData, "ObjectiveStatus", status);
			if ($F.isDate(startTime)) {
				a.setValue(eoBoundData, "workstarttime", startTime);
			} else {
				a.setValue(eoBoundData, "workstarttime", $PublicFun.now());
			}
			a.events[id] = eoBoundData;
		},

		setValue : function(boundData, field, value) {
			var a = this;
			field = field.toLowerCase();
			if (a.fields[field]) {
				let fieldType = $CONST.BoundType.F_String;
				if (a.fields[field].type === 'D') {
					fieldType = $CONST.BoundType.F_Date;
				} else if (a.fields[field].type === 'I') {
					fieldType = $CONST.BoundType.F_Int;
				} else if (a.fields[field].type === 'N') {
					fieldType = $CONST.BoundType.F_Float;
				}
				boundData.NewValueEdit(field, value, fieldType);
			} else
				$F.err('EventObjective - setValue', '字段[' + field + ']不存在');
		},

		save : function(boundData, status) {
			var a = this, i = 0;
			var allSubs = boundData.allSubs;
            for(var subId in allSubs){
                if(subId.startWith('eventObjective')){
                    delete allSubs[subId];
                }
            }
			for (var key in a.events) {
                if(!a.events[key]._hasSaved){
                    a.setValue(a.events[key], 'ObjectiveStatus', status);
                    boundData.SubTableAdd("eventObjective" + key, a.events[key]);
                    a.events[key]._hasSaved = true;
                }
			}
		}
        ,hasEvents : function(){
            return !$.isEmptyObject(this.events);
        }

	};

	return {
		/**
		 * 创建一个eventObjective对象
		 * 
		 * @method create
		 * @param project
		 *            {object} 项目对象
		 * @param extTBName
		 *            {string} 历史备份的数据的表后缀
		 * @return {eventObjective} eventObjective对象
		 */
		create : function(project, extTBName) {
			// 读取EventObjective字典
			var fields = project.getCache('EVENTOBJECTIVE_FIELDS');
			if (!fields) {
				fields = {};
				// 加载EventObjective字段列表
				$F.dataService('SystemDo3', {
					data : {
						token : project.token,
						dbPool : '',
						sqls : [ "EventObjective" + $E.staff.epidSql ]
					},
					async : false,
					digest: project.digest,
					success : function(data) {
						if (data.code > 0) {
							// console.log(data.value);
							// 获取objective字段名
							var columns = data.value[0].columns;
							for (var i = 0; i < columns.length; i++) {
								fields[columns[i][0].toLowerCase()] = {
									length : columns[i][3],
									type : columns[i][2]
								}
							}
							project.putCache('EVENTOBJECTIVE_FIELDS', fields);
						} else
							$F.err('EventObjective - create', data.message);
					}
				})
			}
			return new eventObjective(project, fields, extTBName);
		}
	}
});
